package lumis.service.wsrp.producer;

import javax.persistence.NoResultException;

import lumis.doui.processaction.ProcessActionHandler;
import lumis.doui.source.TabularSource;
import lumis.doui.source.field.ITabularSourceField;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.http.IHttpAuthenticator;
import lumis.portal.manager.ManagerFactory;
import lumis.service.wsrp.common.WSRPUtils;

import com.sun.portal.wsrp.producer.ProducerException;
import com.sun.portal.wsrp.producer.ProducerVersion;

/**
 * {@link ProcessActionHandler} responsible for persisting the
 * {@link ProducerImpl} associated with the current service instance.
 * 
 * @version $Revision: 12680 $ $Date: 2011-03-04 15:32:40 -0300 (Fri, 04 Mar 2011) $
 * @since 5.5.0
 */
public class ProducerConfigurationInterfaceProcessActionHandler extends ProcessActionHandler<TabularSource<ITabularSourceField>>
{
	public void processAction() throws PortalException
	{
		String serviceInstanceId = douiContext.getRequest().getServiceInstanceConfig().getId();

		// form values
		String version = getParameter("version", String.class);
		String producerKey = getParameter("producerKey", String.class);
		Boolean enabled = getParameter("enabled", Boolean.class);
		Boolean requiresRegistration = getParameter("requiresRegistration", Boolean.class);
		Boolean supportsInbandRegistration = getParameter("supportsInbandRegistration", Boolean.class);
		Long maxInbandLifetime = getParameter("maxInbandLifetime", Long.class);
		String httpAuthenticator = getParameter("httpAuthenticator", String.class);
		if (httpAuthenticator != null)
		{
			try
			{
				Class<?> clazz = ManagerFactory.getDeploymentManager().getClassLoader().loadClass(httpAuthenticator);
				if (!IHttpAuthenticator.class.isAssignableFrom(clazz))
					throw new PortalException("INVALID_CLASS;" + httpAuthenticator, getResource());
			}
			catch (ClassNotFoundException e)
			{
				throw new PortalException("CLASS_NOT_FOUND;" + httpAuthenticator, getResource(), e);
			}
		}

		if (version == null || producerKey == null || enabled == null )
			throw new PortalException(localize("STR_ALL_FIELDS_ARE_REQUIRED"));
		
		if(supportsInbandRegistration && maxInbandLifetime == null)
			throw new PortalException(localize("STR_SUPPORT_INBAND_MAX_LIFETIME"));

		// gets the producer associated with the current service instance
		ProducerImpl producerImpl;
		try
		{
			producerImpl = (ProducerImpl) ManagerFactory.getEntityManager().createNamedQuery(ProducerImpl.NAMED_QUERY_FIND_PRODUCER_BY_NAMESPACE).setParameter("namespace", serviceInstanceId)
					.getSingleResult();
		}
		catch (NoResultException e)
		{
			producerImpl = null;
		}

		if (producerImpl == null)
			producerImpl = new ProducerImpl();
			
		
		try
		{
			producerImpl.setIsEnabled(enabled);
			producerImpl.setNamespace(serviceInstanceId);
			producerImpl.setProducerKey(producerKey);
			producerImpl.setVersion(ProducerVersion.fromValue(version));
			producerImpl.setHttpAuthenticator(httpAuthenticator);
			producerImpl.setRequiresRegistration(requiresRegistration);
			producerImpl.setInbandRegistrationSupported(supportsInbandRegistration);
			if(supportsInbandRegistration)
			{
				producerImpl.setMaxInbandLifetime(maxInbandLifetime * WSRPUtils.MILISSECONDS_IN_A_DAY);
			}
			else
			{
				producerImpl.setMaxInbandLifetime(0);
			}

		}
		catch (ProducerException e)
		{
			throw new UnexpectedException(e);
		}

		
		if (producerImpl.getId() == null)
			ManagerFactory.getEntityManager().persist(producerImpl);

		addDefaultResponse();
	}
}
